package com.aynu.controller;

import com.aynu.Service.AnimationCharacterService;
import com.aynu.entities.AnimationCharacter;
import com.aynu.entities.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController//不加80窗口会出错
@Slf4j
public class AnimationCharacterController {

    @Resource
    AnimationCharacterService animationCharacterService;

    @Value("${server.port}")
    private String serverPort;

    @RequestMapping("/aaa")
    public void hello(){
        System.out.println("测试hello");
        log.info("查询数据结果");
//        return "aaa";
    }
    //CommonResult只给前端返回这个类型的数据
    @PostMapping(value = "animation/creat")
    //注意这里是PostMapping注解，且下面一行要加@RequestBody注解，这样80就会得到数值
    public CommonResult create(@RequestBody AnimationCharacter animationCharacter){
        System.out.println(" public CommonResult create8001contrller"+animationCharacter);
        int result = animationCharacterService.create(animationCharacter);
        log.info("插入数据结果"+result);
        if (result>0){
//            return new CommonResult(200,"插入数据库成功",result);
            return new CommonResult(200,"插入数据库成功,serverPort: "+serverPort,result);
        }else {
            return new CommonResult(444,"插入数据库失败",null);
        }

    }

// 加上注解@PathVariable("id")
    @GetMapping(value = "animation/get/{id}")
    public CommonResult getAnimationById(@PathVariable("id") Long id){
        AnimationCharacter animationCharacter1 = animationCharacterService.getAnimationCharacterById(id);
        log.info("插入数据结果"+animationCharacter1);
        if (animationCharacter1 != null){
            return new CommonResult(200,"查询成功",animationCharacter1);
        }else {
            return new CommonResult(444,"查询失败，无此记录",null);
        }
    }

}
